myApp.controller('returnGoods', ['$scope', '$http', function($scope, $http){
	$scope.saleOrderNo = "";
	$scope.returnGoodsItem = [];
	$scope.returnGoodsList = [];
	$scope.total = 0;

	$scope.searchItemBySaleOrderNo = function() {
		$scope.returnGoodsItem = [];
		$scope.returnGoodsList = [];
		$scope.total = 0;

		if($scope.saleOrderNo === "") {
			alert("请填写销售单号！");
			return;
		}

		var data = {"saleOrderNo": $scope.saleOrderNo};

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('searchItemBySaleOrderNo', data, postCfg).then(function successCallback(response) {
			$scope.returnGoodsItem = response.data;
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.addToList = function(item) {
		for (var i = 0; i < $scope.returnGoodsList.length; i++) {
			if(item.barcode === $scope.returnGoodsList[i].barcode) {
				if(item.quantity === $scope.returnGoodsList[i].returnQuantity) {
					alert("退货数量不得大于购买数量！");
					return;
				}
				$scope.returnGoodsList[i].returnQuantity += 1;
				$scope.returnGoodsList[i].returnSubTotal = $scope.returnGoodsList[i].returnQuantity * $scope.returnGoodsList[i].salePrice;
				$scope.total += $scope.returnGoodsList[i].salePrice;
				return;
			}
		}
		item.returnQuantity = 1;
		item.returnSubTotal = item.salePrice;
		$scope.returnGoodsList.push(item);
		$scope.total += item.salePrice;
	};

	$scope.deleteFromList = function(item, index) {
		if(item.returnQuantity === 1) {
			$scope.returnGoodsList.splice(index, 1);
		}else {
			$scope.returnGoodsList[index].returnQuantity -= 1;
			$scope.returnGoodsList[index].returnSubTotal = $scope.returnGoodsList[index].returnQuantity * $scope.returnGoodsList[index].salePrice;
		}
		$scope.total -= item.salePrice;
	};

	$scope.processReturns = function() {

		var returnGoods = {};
		returnGoods.returnGoodsNo = $scope.returnGoodsList[0].saleOrderNo;
		returnGoods.customerNo = "";
		returnGoods.salespersonNo = "";
		returnGoods.shopId = 0;
		returnGoods.total = $scope.total;
		returnGoods.remark = "";

		var returnGoodsItemsJson = [];
		for (var i = 0; i < $scope.returnGoodsList.length; i++) {
			var item = {};
			item.returnGoodsNo = $scope.returnGoodsList[i].saleOrderNo;
			item.barcode = $scope.returnGoodsList[i].barcode;
			item.quantity = $scope.returnGoodsList[i].returnQuantity;
			item.returnPrice = $scope.returnGoodsList[i].salePrice;
			item.subTotal = $scope.returnGoodsList[i].returnSubTotal;	
			returnGoodsItemsJson.push(item);		
		}

		var data = {"returnGoods.returnGoodsNo": $scope.returnGoodsList[0].saleOrderNo,
		"returnGoods.customerNo": "",
		"returnGoods.salespersonNo": "",
		"returnGoods.shopId": 0,
		"returnGoods.total": $scope.total,
		"returnGoods.remark": "",
		"returnGoodsItemsJson": JSON.stringify(returnGoodsItemsJson)
	};

	var postCfg = {
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
		transformRequest: function (data) {
			return $.param(data);
		}
	};

	$http.post('processReturns', data, postCfg).then(function successCallback(response) {
		alert("已退货成功！");
		$scope.saleOrderNo = "";
		$scope.returnGoodsItem = [];
		$scope.returnGoodsList = [];
		$scope.total = 0;
		console.log("ok：" + response);
	}, function errorCallback(response) {
		alert("退货操作失败!");
		console.log("fail：" + response);
	});


};

}]);